import { Component, OnInit } from "@angular/core";
import { JsonApiService } from "app/core/api/json-api.service";
import { NotificationService } from "../../../shared/utils/notification.service";
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

declare var layui: any;
declare var xmSelect: any;
@Component({
  selector: "sa-order-monitor",
  templateUrl: "./order-monitor.component.html",
  styleUrls: ["./order-monitor.css"]
})
export class OrderMonitorComponent implements OnInit {
  constructor(
    private jsonApiService: JsonApiService,
    private notificationService: NotificationService,
    private router: Router,
    private activatedRoute: ActivatedRoute
  ) {
    activatedRoute.queryParams.subscribe((queryParams) => {
      if (queryParams) {
        this.jumpData = queryParams;
      }
    });
  }
  jumpData: any;
  tableIns: any;
  dataList = [
    {
      id: 10000,
      username: "user-0"
    },
    {
      id: 10001,
      username: "user-1"
    },
    {
      id: 10002,
      username: "user-2"
    },
    {
      id: 10003,
      username: "user-3"
    },
    {
      id: 10004,
      username: "user-4"
    },
    {
      id: 10005,
      username: "user-5"
    },
    {
      id: 10006,
      username: "user-6"
    },
    {
      id: 10007,
      username: "user-7"
    },
    {
      id: 10008,
      username: "user-8"
    },
    {
      id: 10009,
      username: "user-9"
    },
    {
      id: 10010,
      username: "user-10"
    },
    {
      id: 10011,
      username: "user-11"
    },
    {
      id: 10012,
      username: "user-12"
    },
    {
      id: 10013,
      username: "user-13"
    },
    {
      id: 10014,
      username: "user-14"
    },
    {
      id: 10015,
      username: "user-15"
    },
    {
      id: 10016,
      username: "user-16"
    }
  ];
  tableData: any = []; //table数据
  questionTypeList: any = [{
		value: '1',
		name: '算法问题',
		children: [{
			value: '101',
			name: '上货问题',
		}, {
			id: '104',
			label: '多次上货',
		},{
			value: '102',
			name: '卸货问题',
		}, {
			id: '105',
			label: '多次卸货',
		}, {
			value: '103',
			name: '在途问题',
		}, {
			value: '100',
			name: '其他',
		}]
	}, {
		value: '2',
		name: '硬件问题',
		children: [{
			value: '201',
			name: '主机问题',
		}, {
			value: '202',
			name: '传感器问题',
		}, {
			value: '203',
			name: 'GPS问题',
		}, {
			value: '204',
			name: '线材问题',
		}, {
			value: '200',
			name: '其他',
		}]
	}, {
		value: '3',
		name: '司机问题',
		children: [{
			value: '301',
			name: '司机问题',
		}, {
			value: '300',
			name: '其他',
		}]
	}, {
		value: '4',
		name: '对接问题',
		children: [{
			value: '401',
			name: '对接问题',
		}, {
			value: '400',
			name: '其他',
		}]
	}, {
		value: '5',
		name: '运维问题',
		children: [{
			value: '501',
			name: '运维问题',
		}, {
			value: '502',
			name: '未升级',
		}, {
			value: '503',
			name: '系数问题',
		}, {
			value: '500',
			name: '其他',
		}]
	}, {
		value: '6',
		name: '安装问题',
		children: [{
			value: '601',
			name: '安装问题',
		}, {
			value: '600',
			name: '其他',
		}]
	}, {
		value: '7',
		name: '平台问题',
		children: [{
			value: '701',
			name: '未识别异常',
		}, {
			value: '702',
			name: '算法曲线断裂',
		}, {
			value: '703',
			name: '平台卸货又上货',
		}, {
			value: '700',
			name: '其他',
		}]
	}, {
		value: '8',
		name: '磅单问题',
		children: [{
			value: '801',
			name: '磅单问题',
		}, {
			value: '800',
			name: '其他',
		}]
	},{
		value: '9',
		name: '断电问题',
		children: [{
			value: '901',
			name: '断电问题',
		}]
	}];
  questionNameList: any = {
		'1': '算法问题',
		'2': '硬件问题',
		'3': '司机问题',
		'4': '对接问题',
		'5': '运维问题',
		'6': '安装问题',
		'7': '平台问题',
		'8': '磅单问题',
		'9': '断电问题',
	};
  questionDeatailNameList: any = {
		'100': '其他',
		'101': '上货问题',
		'102': '卸货问题',
		'103': '在途问题',
    '104': '多次上货',
		'105': '多次卸货',
		'200': '其他',
		'201': '主机问题',
		'202': '传感器问题',
		'203': 'GPS问题',
		'204': '线材问题',
		'300': '其他',
		'301': '司机问题',
		'400': '其他',
		'401': '对接问题',
		'500': '其他',
		'501': '运维问题',
		'502': '未升级',
		'503': '系数问题',
		'601': '安装问题',
		'600': '其他',
		'701': '未识别异常',
		'702': '算法曲线断裂',
		'703': '平台卸货又上货',
		'700': '其他',
		'801': '磅单问题',
		'800': '其他',
		'901': '断电问题',
	}
  tabNumberList: any =[];
  btnNum: string = 'all';

  //查询存储值
  dateType: any; // 时间类型
  orderStatusForSelect: any; //订单进度
  checkStatusForSelect: any; //订单状态
  detailStatus: any; //处理状态
  isFixWeightForSelect: any; //算法
  useTypeL2: any; //车辆分类
  isGpsErrorForSelect: any; //GPS状态
  algorithmProblemTypes: any; //算法问题分类
  questionTypeSelect2: any; //选择天数
  selectedCompanyId: any; //选择公司
  selectedDeviceId: any; //选择车辆
  selectedCarNumber: any;
  programType: any; //问题分类
  
  startTime: any;
  endTime: any;

  ngOnInit() {
    var that = this;
    that.obtainCompanyData(); //获取公司数据


    // 初始选择器
    that.initSelect();
      // 初始layui
    that.initLayui();

    that.load();
  }

  initLayui() {
    const that = this;
    layui.use(function () {
      const table = layui.table;
      const layer = layui.layer;
      const form = layui.form;
      const date = layui.laydate;
      const element = layui.element;
      that.tableIns = table;
      
      // 初始表单元素
      form.render();
      // 初始选项卡元素
      element.render("tab");

      var getTime = common.getRecentDaysDateTime(that.questionTypeSelect2.getValue('valueStr'));
      date.render({
        elem: "#dateRange",
        type: "datetime",
        range: ['#startDate', '#endDate'],
        done: function (value, date, endDate) {
          //   console.log(value); //得到日期生成的值，如：2017-08-18
          //   console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
          //   console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
          that.startTime = value.split(' - ')[0];
          that.endTime = value.split(' - ')[1];
        },
        max: getTime.endTime
      });
      $('#startDate').val(getTime.startTime);
      $('#endDate').val(getTime.endTime)
      // 初始表格元素
      that.initTable(table);
      // 监听选项卡事件
      element.on("tab(docDemoTabBrief)", function (data) {
        console.log(data.index); //得到当前Tab的所在下标
      });

      form.on('submit(formDemo)', function (data) {
        console.log(data.field);
        that.load()
      });

      
    });
  }

  
  initTable(table) {
    const that = this;
    table.render({
      elem: "#demo",
      title: "订单监控",
      page: true, //开启分页
      data: that.tableData,
      cellMinWidth: 120,
      cols: [
        [
          // { checkbox: true },
          { field: "orderStatusJudge", title: "状态",  align: "center"}, //需要 判断处理
          { field: "detailStatusJudge", title: "处理状态", align: "center" }, //需要 判断处理
          { field: "detailUserName", title: "操作人", align: "center" },
          { field: "carNumber", title: "车牌号码", align: "center" },
          { field: "exceptionType", title: "问题分类", align: "center" },
          { field: "programTypeKeyJudge", title: "现象分类", align: "center" },//需要 判断处理
          { field: "detailTime", title: "操作时间", align: "center" },
          { field: "emptyLoadTime", title: "进厂时间", align: "center" },
          { field: "companyName", title: "公司名称", align: "center" },
          { field: "corname", title: "客户", align: "center" },
          { field: "algorithmProcessingStatusJudge", title: "处理状态[算法]	", align: "center" },//需要 判断处理
          { field: "algorithmProblemTypes", title: "问题类型[算法]", align: "center" },
          { field: "algorithmNotes", title: "备注[算法]	", align: "center" },
          { field: "algorithmProcessor", title: "处理人[算法]	", align: "center" },
          { field: "isComplaintJudge", title: "是否投诉", align: "center" }, //需要 判断处理
          { field: "exceptionRemark", title: "异常描述", align: "center" },
          {
            title: "操作",
            fixed: "right",
            align: "center",
            toolbar: `<div id="barDemo">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="operation">处理[算法]</a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">查看</a>
                        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="gps">GPS修复</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
                      </div>`
          }
        ]
      ],
      // toolbar: true,
      even: true, //开启隔行背景
      id: "idTest"
    });

    table.on("tool(test)", function (obj) {
      const data = obj.data;
      const layEvent = obj.event;
      const tr = obj.tr;
    });

    table.on("checkbox(test)", function (obj) {
      console.log(obj);
    });

    //触发行双击事件
    table.on("rowDouble(test)", function (obj) {
      //obj 同上
      console.log(obj);
    });
  }

  load() {
    
		let programType = this.programType.getValue('valueStr');

		let timeCheck = common.correctTimeParams(this.startTime, this.endTime, 0);
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		const param = {
			"parameters": {
				deviceId: this.selectedDeviceId.getValue('valueStr'),
				startTime: this.startTime,
				endTime: this.endTime,
				detailUserName: $('#userName').val(),
				companyId: this.selectedCompanyId.getValue('valueStr'),
				carNumber: this.selectedCarNumber,
				dateType: this.dateType.getValue('valueStr'),
				searchKey: $('#searchKey').val(),
        orderStatus: '',
				orderStatusForSelect: this.orderStatusForSelect.getValue('valueStr'),
        checkStatus:'',
				checkStatusForSelect: this.checkStatusForSelect.getValue('valueStr'),
				detailStatus: this.detailStatus.getValue('valueStr'),
				isFixWeightForSelect: this.isFixWeightForSelect.getValue('valueStr'),
				useTypeL2: this.useTypeL2.getValue('valueStr'),
				isWaitDetail: '0',
				exceptionType: '',
				isMatlab: '',
				isRail: '',
				secondRecord: '',
				algorithmProblemTypes: this.algorithmProblemTypes,
				programType: programType,
				isUnusualOrder: '',
				isGpsErrorForSelect: this.isGpsErrorForSelect.getValue('valueStr'),
				sensorAbnormal: '',
				algorithmProcessingIsNormal: '',
			}
		};
		switch (this.btnNum) {
			case 'all':
				param.parameters.isWaitDetail = '';
				param.parameters.orderStatus = '';		//全部
				break;
			case 'trans': //运输中
				param.parameters.orderStatus = '0';
        param.parameters.isWaitDetail = '0';
				break;
			case 'normal': //正常卸货
				param.parameters.checkStatus = '0';
				param.parameters.orderStatus = '1';
        param.parameters.isWaitDetail = '0';
				break;
			case 'deviceOffLine':
				param.parameters.checkStatus = '6';		//设备离线
        param.parameters.isWaitDetail = '0';
				break;
			case 'systemMisjudgment':
				param.parameters.exceptionType = '7';		//平台问题
        param.parameters.isWaitDetail = '0';
				break;
			case 'weightError':
				param.parameters.exceptionType = '8';		//磅单问题
        param.parameters.isWaitDetail = '0';
				break;
			case 'blackout':
				param.parameters.exceptionType = '9';		//断电问题
        param.parameters.isWaitDetail = '0';
				break;
			case 'waitDeal': //待处理
				param.parameters.isWaitDetail = '1';
				break;
			case 'algorithm':
				param.parameters.exceptionType = '1';		//算法问题
        param.parameters.isWaitDetail = '0';
				break;
			case 'hwVersion':
				param.parameters.exceptionType = '2';		//硬件问题
        param.parameters.isWaitDetail = '0';
				break;
			case 'MatLab':
				param.parameters.isMatlab = '1';		//matlab问题
				// param.parameters.algorithmProblemTypes = this.algorithmProblemTypes.getValue('valueStr'); //算法问题分类
        param.parameters.isWaitDetail = '0';
				break;
			case 'secondLoading':
				param.parameters.secondRecord = '1';		//二次上货
        param.parameters.isWaitDetail = '0';
				break;
			case 'preOrder': //预审失败
        param.parameters.isWaitDetail = '0'
				param.parameters.isUnusualOrder = '1';
				break;
			case 'gpsErrorSensorNormal':
				param.parameters.checkStatus = '1';      //gps异常
				param.parameters.sensorAbnormal = '0';   //传感器正常
				break;
			case 'gpsErrorSensorAbnormal':
				param.parameters.checkStatus = '1';      //gps异常
				param.parameters.sensorAbnormal = '1';   //传感器异常
        param.parameters.isWaitDetail = '0'
				break;
			case 'dataErrorSensorNormal':
				param.parameters.checkStatus = '2';     //数据异常
				param.parameters.sensorAbnormal = '0';   //传感器正常
				param.parameters['isGpsErrorT'] = '0';   //GPS正常
        param.parameters.isWaitDetail = '0'
				break;
			case 'dataErrorSensorAbnormal':
				// param.parameters.checkStatus = '2';     //数据异常
				param.parameters.sensorAbnormal = '1';   //传感器异常
				param.parameters['isFixWeightT'] = "2"; // 算法未识别上货
				param.parameters['isGpsErrorT'] = '0';   //GPS正常
        param.parameters.isWaitDetail = '0'
				break;
			case 'plateNormal':
				param.parameters.algorithmProcessingIsNormal = '1';   //平台处理-正常卸货
				param.parameters['isGpsErrorT'] = '0';   //GPS正常
        param.parameters.isWaitDetail = '0'
				break;
			case 'plateHalf': // 平台处理-运输中
				param.parameters.orderStatus = '0';     //运输中
				// param.parameters.sensorAbnormal = '0';   //传感器正常
				param.parameters['isFixWeightT'] = "4"; // 已运行算法
				param.parameters['isGpsErrorT'] = '0';   //GPS正常
        param.parameters.isWaitDetail = '0'
				break;
			default:
				break;
		}
		let reqdata = 'gridPager=' + JSON.stringify(param);
		// this.loadSortData(reqdata);
		this.jsonApiService.http("orderController3/findOrderList.html", reqdata).subscribe(data => {
			if (data && data.exhibitDatas) {
				for (let item of data.exhibitDatas) {
					item.sendWeight = common.twoDecimal(item.sendWeight);
					item.loadTime = common.secondToDate(item.loadTime);
					item.unloadTime = common.secondToDate(item.unloadTime);
					item.transportTime = common.secondToDate(item.transportTime);
					item.exceptionType = this.getExceptionTypeName(item.exceptionType);
					item['programTypeKey'] = item.programType
					item.programType = this.getProgramTypeName(item.programType);


          //处理前台逻辑

          //第一列
          if(item.orderStatus == 0 && item.checkStatus == 0 && (!item.isGpsError || item.isGpsError == 2 )&& this.btnNum != 'abnormal'){
            item.orderStatusJudge = `<span class="orange">运输中</span>`;
          }
          if(item.orderStatus !=0 && item.checkStatus == 0 && (!item.isGpsError || item.isGpsError == 2 ) && this.btnNum != 'abnormal'){
            item.orderStatusJudge = `<span class="green">正常卸货</span>`;
          }
          if(item.isGpsError == 1 && this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal'){
            item.orderStatusJudge = `<span class="red">GPS异常</span>`;
          }
          if(item.isGpsError == 3 && this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal'){
            item.orderStatusJudge = `<span class="red">停车速度平直</span>`;
          }
          if(item.isGpsError == 5 && this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal'){
            item.orderStatusJudge = `<span class="red">大段丢失</span>`;
          }
          if(item.isGpsError == 6 && this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal'){
            item.orderStatusJudge = `<span class="red">偶尔丢失</span>`;
          }
          if(this.btnNum == 'abnormal'){
            item.orderStatusJudge = `<span class="red">异常卸货</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 2){
            item.orderStatusJudge = `<span class="red">未清零 [进厂]</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 3){
            item.orderStatusJudge = `<span class="red">未识别上货 [离厂]</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 4){
            item.orderStatusJudge = `<span class="red">重量突变 [在途]</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 5){
            item.orderStatusJudge = `<span class="red">卸货未清零</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 6){
            item.orderStatusJudge = `<span class="red">设备离线</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 7){
            item.orderStatusJudge = `<span class="red">平台问题</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 8){
            item.orderStatusJudge = `<span class="red">垂直卸货</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 9){
            item.orderStatusJudge = `<span class="red">磅单问题</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 10){
            item.orderStatusJudge = `<span class="red">异常订单</span>`;
          }
          if(this.btnNum != 'abnormal' && this.btnNum != 'dataErrorSensorNormal' && this.btnNum != 'dataErrorSensorAbnormal' && (!item.isGpsError || item.isGpsError == 2 ) && item.checkStatus == 11){
            item.orderStatusJudge = `<span class="red">卸货有速度</span>`;
          }

          
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 2){
            item.orderStatusJudge = `<span class="red">未清零 [进厂]</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 3){
            item.orderStatusJudge = `<span class="red">未识别上货 [离厂]</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 4){
            item.orderStatusJudge = `<span class="red">重量突变 [在途]</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 5){
            item.orderStatusJudge = `<span class="red">卸货未清零</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 6){
            item.orderStatusJudge = `<span class="red">设备离线</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 7){
            item.orderStatusJudge = `<span class="red">平台问题</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 8){
            item.orderStatusJudge = `<span class="red">垂直卸货</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 9){
            item.orderStatusJudge = `<span class="red">磅单问题</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 10){
            item.orderStatusJudge = `<span class="red">异常订单</span>`;
          }
          if((this.btnNum == 'dataErrorSensorNormal' || this.btnNum == 'dataErrorSensorAbnormal') && item.checkStatus == 11){
            item.orderStatusJudge = `<span class="red">卸货有速度</span>`;
          }

          //第二处
          if(item.detailStatus === 0){
            item.detailStatusJudge = `<span class="orange">待处理</span>`
          }
          if(item.detailStatus === 1){
            item.detailStatusJudge = `<span class="green">人工处理</span>`
          }
          if(item.detailStatus === 2){
            item.detailStatusJudge = `<span class="green">算法处理</span>`
          }
          if(item.detailStatus === 3){
            item.detailStatusJudge = `<span class="green">无异常</span>`
          }

          //第三处
          if(item.programTypeKey!=700){
            item.programTypeKeyJudge = item.programType
          }else{
            item.programTypeKeyJudge = item.programType+item.platformIssueDescription?":"+item.platformIssueDescription:""
          }

          //第四处
          if(item.algorithmProcessingStatus === 0){
            item.detailStatusJudge = "未解决"
          }
          if(item.algorithmProcessingStatus === 1){
            item.detailStatusJudge = "已解决"
          }
          if(item.algorithmProcessingStatus === 2){
            item.detailStatusJudge = "其他"
          }

          //第五处
          if(item.isComplaint == 1){
            item.isComplaintJudge = "被投诉"
          }else{
            item.isComplaintJudge = ""
          }
				}
				this.tableData = data.exhibitDatas;
			}
		})
	}

  //数据分类按钮点击事件
	btnClick(type) {
    var that = this;
		this.btnNum = type;
    $("#MatLabSelct").css('display','inline-block');
    if(type == 'MatLab'){
      //算法问题分类 只有在Matlab算法问题 tap的时候显示
      xmSelect.render({
        el: "#algorithmProblemTypes",
        radio: true,
        clickClose: true,
        model: {
          icon: "hidden",
          label: {
            type: "text"
          }
        },
        data: [
          { name: "全部", value: '' },
          { name: "算法问题", value: '算法问题'},
          { name: "传感器问题", value: '传感器问题' },
          { name: "系数问题", value: '系数问题' },
          { name: "对接问题", value: '对接问题' },
          { name: "空", value: '无' },
          { name: "其他", value: '其他' }
        ],
        on: function(data){
          that.algorithmProblemTypes = data.arr[0].value
        }
      });
      
    }else{
      $("#MatLabSelct").css('display','none');
    }
		this.load();
	}

  //现象分类
	getProgramTypeName(key) {
		if (key) {
			return this.questionDeatailNameList[key];
		} else {
			return '';
		}
	}
	//问题分类
	getExceptionTypeName(key) {
		if (key) {
			return this.questionNameList[key];
		} else {
			return '';
		}
	}

  initSelect() {
    var that = this;
    //公司
    that.selectedCompanyId = xmSelect.render({
      el: '#companyId', 
      filterable: true,
      pageEmptyShow: false,
      //单选模式
      radio: true,
      tree: {
        show: true,//是否显示树状结构
        showFolderIcon: true, //是否展示三角图标
        showLine: true, //是否显示虚线
        indent: 10, //间距
        expandedKeys: true, //默认展开节点的数组, 为 true 时, 展开所有节点
        strict: false, //重点！！设置成非严格父子模式，这样父节点被禁用，子节点依然可以点击
      },
      toolbar: {
        show: true,
        list: ['ALL','CLEAR']
      },
      model: {
        icon: "\f015",
        label: {
          type: "text"
        }
      },
      height: '500px',
      data: []
    })

    //车辆分类修改数组key的名字
    var data1 = carTypeListObj.carTypeList
    var data2 = this.replaceKey(data1, 'label');
    //车辆分类
    this.useTypeL2 = xmSelect.render({
      el: "#useTypeL2",
      filterable: true,
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: data2
    });

    //车牌号码
    this.selectedDeviceId = xmSelect.render({
      el: "#deviceId",
      autoRow: true,
      //单选模式
      radio: true,
      clickClose: true,
      toolbar: { show: true },
      filterable: true,
      remoteSearch: true,
      remoteMethod: function(val, cb, show){
        //这里如果val为空, 则不触发搜索
        if(!val){
          return cb([]);
        }
        var carNumber = {"carNumber":val}
        that.jsonApiService.http_obj("stats/carListByCarNumber.html",carNumber).subscribe(data => {
          if (data) {
            var res = data.data;
            let newArrs = res.reduce((pre,cur,index)=>{
              if( (pre[index-1] && pre[index-1].name ) == cur.carNumber){
                cur.value = cur.deviceId;
                cur.name = cur.carNumber + '['+ cur.parentName+ ']';
                return  pre.concat(cur)
              }else{
                cur.value = cur.deviceId;
                cur.name = cur.carNumber;
                return pre.concat(cur)
              }
          },[])

            cb(newArrs)
          }
        })
      },
      on: function(data){
        if(data.arr.length != 0){
          that.selectedCarNumber = data.arr[0].name;
        }
      }
    });

    //问题分类
    this.programType = xmSelect.render({
      el: "#programType",
      filterable: true,
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: this.questionTypeList
    });

    //处理状态
    this.detailStatus = xmSelect.render({
      el: "#detailStatus",
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: [
        { name: "全部", value: '' },
        { name: "待处理", value: 0 },
        { name: "人工处理", value: 1 },
        { name: "算法处理", value: 2 },
        { name: "无异常", value: 3 }
      ]
    });

    //GPS状态
    this.isGpsErrorForSelect = xmSelect.render({
      el: "#isGpsErrorForSelect",
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: [
        { name: "全部", value: '' },
        { name: "GPS问题", value: 1 },
        { name: "无GPS", value: 3 },
        { name: "停车车速平直", value: 4 },
        { name: "大段丢失", value: 5 },
        { name: "偶尔丢失", value: 6 }
      ]
    });

    //订单状态
    this.checkStatusForSelect = xmSelect.render({
      el: "#checkStatusForSelect",
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: [
        { name: "全部", value: '' },
        { name: "正常卸货", value: 0 },
        { name: "未清零 [进厂]", value: 2 },
        { name: "未识别上货 [离厂]", value: 3 },
        { name: "重量突变 [在途]", value: 4 },
        { name: "卸货未清零", value: 5 },
        { name: "设备离线", value: 6 },
        { name: "垂直卸货", value: 8 },
        { name: "异常订单", value: 10 },
        { name: "卸货有速度", value: 11 }
      ]
    });

    //订单进度
    this.orderStatusForSelect = xmSelect.render({
      el: "#orderStatusForSelect",
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: [
        { name: "全部", value: '' },
        { name: "未结束", value: 0 },
        { name: "已结束", value: 1 }
      ]
    });

    //算法
    this.isFixWeightForSelect = xmSelect.render({
      el: "#isFixWeightForSelect",
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: [
        { name: "全部", value: '' },
        { name: "未运行算法", value: 0 },
        { name: "算法正常运行", value: 1 },
        { name: "算法未识别上货", value: 2 },
        { name: "算法未清零", value: 3 },
        { name: "算法已运行", value: 4 }
      ]
    });

    //时间类型
    this.dateType = xmSelect.render({
      el: "#dateType",
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: [
        { name: "进厂时间", value: 1, selected: true},
        { name: "离厂时间", value: 2 },
        { name: "订单结束时间", value: 3 },
        { name: "操作时间", value: 4}
      ]
    });

    //选择天数
    this.questionTypeSelect2 = xmSelect.render({
      el: "#questionTypeSelect2",
      radio: true,
      clickClose: true,
      model: {
        icon: "hidden",
        label: {
          type: "text"
        }
      },
      data: [
        { name: "请选择", value: '' },
        { name: "近10天[默认]", value: 10 ,selected: true},
        { name: "近30天", value: 30 },
        { name: "近60天", value: 60 },
        { name: "近90天", value: 90 },
        { name: "近100天", value: 100 },
      ],
      on: function(data){
        var num = data.arr[0].value
        if(num == ''){
          return;
        }else{
          var dateObj = common.getRecentDaysDateTime(num);
          that.startTime = dateObj.startTime;
          that.endTime = dateObj.endTime;

          $('#startDate').val(dateObj.startTime);
          $('#endDate').val(dateObj.endTime)
        }
        
      }
    });

  }

  //订单分类汇总
	loadSortData(reqdata) {
		this.jsonApiService.http("orderController3/countTypeTab.html", reqdata).subscribe(data => {
			if (data && data.data) {
				this.tabNumberList = data.data;
			}
		})
	}

  //获取公司数据
  obtainCompanyData() {
    var that = this;
    that.jsonApiService.http("company/companytree.html", "").subscribe(data => {
      if (data) {
        // 构建好的树形结构，其中仅包含dataList
        var treeData = [];
        var i = 0;
        // 赋值第一级
        data.forEach((element) => {
          if (element.parent === '#') {
            treeData[i] = element
            treeData[i].value = element.id;
            treeData[i].name = element.text;
            i++
          }
        });
        // 递归剩余层级
        if(treeData.length != 0){
          treeData.forEach((temp) => {
            that.getTreeData(data, temp)
          })
          that.selectedCompanyId.update({
            data: treeData,
            autoRow: true,
          });
        }
        
      }
    })
	}

  getTreeData(list,data) {
    var i = 0
    var childData = [];
    list.forEach((element) => {
      // 层级相同，且父级一致
      if (data.id === element.parent) {
        childData[i] = element;
        childData[i].value = element.id;
        childData[i].name = element.text;
        i++
        this.getTreeData(list, element)// 递归
      }
    })
    // 赋值数据
    data.children = childData;
  }

  replaceKey(data1,keyName) {
    data1.map((value,index,arry)=>{
      if(value.children && value.children.length != 0){
        value.children.map((item,index,arry)=>{
          if(item.children && item.children.length != 0){
            // var childrened = [];
            item.children.map((list,index,arry)=>{
              if(list.children && list.children.length != 0){

              }else{
                list.name = list[keyName];
                list.value = list.id;
              }
            })
            item.name = item[keyName];
            item.value = item.id;
            item.children = item.children;
          }else{
            item.name = item[keyName];
            item.value = item.id;
          }
        })
        value.name = value[keyName];
        value.value = value.id;
        value.children = value.children;
      }else{
        value.name = value[keyName];
        value.value = value.id;
      }
    })
    return data1;
  }
  onExport() {
    this.tableIns.exportFile("idTest", this.tableData);
  }

  ngOnDestroy() {
    document.onclick = null;
    window.onresize = null;
  }
}
